package com.example.oss.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.example.oss.storage.IStorage;
import com.example.oss.storage.impl.AliyunStorage;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(OSSProperties.class)
public class AliyunOssConfig {

    private final OSSProperties ossProperties;

    public AliyunOssConfig(OSSProperties ossProperties) {
        this.ossProperties = ossProperties;
    }

    @Bean
    public IStorage aliyunStorage(OSSProperties ossProperties){
        OSSProperties.Aliyun aliyun = ossProperties.getAliyun();
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(aliyun.getAccessKeyId(), aliyun.getAccessKeySecret());
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(aliyun.getEndpoint())
                .credentialsProvider(credentialsProvider)
                .build();
        return new AliyunStorage(ossClient);
    }



}
